package com.thinkcube.edutainment.adapters;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.domain.data.CourseData;

public class CourseAdapater extends BaseAdapter {
	List<CourseData> courseList;
	private final DisplayImageOptions options;
	private final ImageLoader imageLoader;

	public CourseAdapater(Context context, List<CourseData> courseList) {
		super();

		this.courseList = courseList;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.icon)
				.showImageForEmptyUri(R.drawable.icon)
				.showImageOnFail(R.drawable.icon).cacheOnDisc(true)
				.displayer(new SimpleBitmapDisplayer())
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
				.bitmapConfig(Bitmap.Config.RGB_565).build(); // default;
		imageLoader = ImageLoader.getInstance();
	}

	@Override
	public int getCount() {
		int size = 0;
		if (courseList != null) {
			size = courseList.size();
		}

		return size;

	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// View retval =
		// LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem,
		// null);
		// TextView title = (TextView) retval.findViewById(R.id.title);
		// Button button = (Button) retval.findViewById(R.id.clickbutton);
		// button.setOnClickListener(mOnButtonClicked);

		ViewHolder holder = null;

		holder = new ViewHolder();
		if (convertView == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.viewitem, null);

			holder.courseName = (TextView) convertView.findViewById(R.id.title);
			holder.teacherPlace = (TextView) convertView
					.findViewById(R.id.teacher_place_view);
			holder.courseImage = (ImageView) convertView
					.findViewById(R.id.imageView1);
			holder.teacherImageView = (ImageView) convertView
					.findViewById(R.id.teacher_place_holder_view);
			holder.teacherName = (TextView) convertView
					.findViewById(R.id.teacher_name_view);
			holder.price = (TextView) convertView
					.findViewById(R.id.course_price);
			holder.studentCount = (TextView) convertView
					.findViewById(R.id.student_count1);
			holder.ratingBar = (RatingBar) convertView
					.findViewById(R.id.ratingBar2);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		// set values
		holder.courseName.setText(courseList.get(position).getCourseTitle());
		holder.studentCount
				.setText(courseList.get(position).getNumOfStudents());
		holder.price.setText(courseList.get(position).getCourseDisplayPrice());
		holder.teacherName.setText(courseList.get(position)
				.getTeacherFullName());
		if (courseList.get(position).getCourseRating() != null) {
			holder.ratingBar.setRating(Float.valueOf(courseList.get(position)
					.getCourseRating()) / 2);
		}

		holder.teacherPlace.setText(courseList.get(position)
				.getTeacherDescription());
		imageLoader.displayImage(courseList.get(position)
				.getCourseCoverImageSnall(), holder.courseImage, options);
		imageLoader.displayImage(courseList.get(position).getTeacherIcon(),
				holder.teacherImageView, options);

		return convertView;
	}

	static class ViewHolder {
		TextView courseName;
		ImageView courseImage;
		ImageView teacherImageView;
		TextView teacherPlace;
		TextView teacherName;
		TextView price;
		TextView studentCount;
		RatingBar ratingBar;
	}
}
